﻿using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;

namespace Project.Core.Config.Config.Jwt {
    public class PermissionRequirementHandler : AuthorizationHandler<PermissionRequirement> {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement) {
            var role = context.User.FindFirst(c => c.Type == ClaimTypes.Role);
            if (role != null) {
                var roleValue = role.Value;
                if (roleValue == requirement.PermissionName) {
                    context.Succeed(requirement);
                }
            }
            return Task.CompletedTask;
        }
    }
}
